home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / xpaint-2.1.1 / typeConvert.c < prev    next >
C/C++ Source or Header  |  1995-05-03  |  3KB  |  115 lines

  1. /* +-------------------------------------------------------------------+ */
  2. /* | Copyright 1993, David Koblas (koblas@netcom.com)                  | */
  3. /* |                                                                   | */
  4. /* | Permission to use, copy, modify, and to distribute this software  | */
  5. /* | and its documentation for any purpose is hereby granted without   | */
  6. /* | fee, provided that the above copyright notice appear in all       | */
  7. /* | copies and that both that copyright notice and this permission    | */
  8. /* | notice appear in supporting documentation.  There is no           | */
  9. /* | representations about the suitability of this software for        | */
  10. /* | any purpose.  this software is provided "as is" without express   | */
  11. /* | or implied warranty.                                              | */
  12. /* |                                                                   | */
  13. /* +-------------------------------------------------------------------+ */
  14.  
  15. #include <X11/IntrinsicP.h>
  16. #include <X11/CoreP.h>
  17. #include <X11/StringDefs.h>
  18. #include "palette.h"
  19.  
  20. static Boolean    newCvtStringToPixel(Display *dpy, XrmValuePtr args,
  21.             Cardinal *nargs, XrmValuePtr from, XrmValuePtr to,
  22.             XtPointer junk)
  23. {
  24.     Screen        *screen;
  25.     Colormap    cmap;
  26.     Palette        *map;
  27.     String        name = (String)(from->addr);
  28.     Boolean        isReverse = False;
  29.     XColor        scol, ecol;
  30.     static Pixel    op;
  31.  
  32.     if (*nargs != 2) 
  33.         return False;
  34.  
  35.     screen = *((Screen **)args[0].addr);
  36.     cmap   = *((Colormap *)args[1].addr);
  37.  
  38.     if (strcmp(name, XtDefaultBackground) == 0)
  39.         name = isReverse ? "black" : "white";
  40.     else if (strcmp(name, XtDefaultForeground) == 0)
  41.         name = isReverse ? "white" : "black";
  42.  
  43.     if ((map = PaletteFindDpy(dpy, cmap)) == NULL) {
  44.  
  45.         /*
  46.         **  Not a "mapped" window, use standard X
  47.         */
  48.         if (XAllocNamedColor(dpy, cmap, name, &scol, &ecol) == 0) {
  49.             if (XParseColor(dpy, cmap, name, &scol) == 0)
  50.                 return False;
  51.             if (XAllocColor(dpy, cmap, &scol) == 0)
  52.                 return False;
  53.         }
  54.  
  55. #if 0
  56. printf("name = %s   match = %d %d %d  screen = %d %d %d\n", 
  57.                     name, ecol.red / 256, ecol.green / 256, ecol.blue / 256,
  58.                          scol.red / 256, scol.green / 256, scol.blue / 256);
  59. #endif
  60.  
  61.         op = scol.pixel;
  62.     } else {
  63.         if (XLookupColor(dpy, map->cmap, name, &scol, &ecol) == 0 &&
  64.             XParseColor(dpy, map->cmap, name, &scol) == 0)
  65.             return False;
  66. #if 0
  67. printf("name = %s   match = %d %d %d  screen = %d %d %d\n", 
  68.                     name, ecol.red / 256, ecol.green / 256, ecol.blue / 256,
  69.                          scol.red / 256, scol.green / 256, scol.blue / 256);
  70. #endif
  71.         op = PaletteAlloc(map, &scol);
  72.     }
  73.  
  74.     if (to->addr == NULL) {
  75.         to->addr = (XtPointer)&op;
  76.     } else {
  77.         if (to->size < sizeof(Pixel))
  78.             return False;
  79.         memcpy(to->addr, &op, sizeof(Pixel));
  80.     }
  81.  
  82.     to->size = sizeof(Pixel);
  83.  
  84.     return True;
  85. }
  86.  
  87. void InitTypeConverters()
  88. {
  89.     static XtConvertArgRec    colorArgs[] = {
  90.             { XtWidgetBaseOffset, 
  91.               (XtPointer)XtOffsetOf(WidgetRec, core.screen),
  92.               sizeof(Screen *) },
  93.             { XtWidgetBaseOffset, 
  94.               (XtPointer)XtOffsetOf(WidgetRec, core.colormap),
  95.               sizeof(Colormap) },
  96.         };
  97.  
  98.     /*
  99.     ** XXX XtCacheNone should be XtCacheRefCount 
  100.     **     but that has a tendency to core dump Xt...
  101.     **     under release 4
  102.     */
  103.  
  104. #if XlibSpecificationRelease > 4
  105. #define CACHE    XtCacheRefCount
  106. #else
  107. #define CACHE    XtCacheNone
  108. #endif
  109.  
  110.     XtSetTypeConverter(XtRString, XtRPixel, 
  111.                 (XtTypeConverter)newCvtStringToPixel,
  112.                 colorArgs, XtNumber(colorArgs),
  113.                 CACHE, NULL);
  114. }
  115.